package cn.com.nes.site.listener;

import cn.com.nes.site.entity.em.RegisterTypeEnum;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.math.BigDecimal;

public class RegisterTypeConverter implements Converter<String> {
    @Override
    public Class supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                    GlobalConfiguration globalConfiguration) {
        for (RegisterTypeEnum registerTypeEnum : RegisterTypeEnum.values()) {
            if (registerTypeEnum.getName().equals(cellData.getStringValue())) {
                return registerTypeEnum.toString();
            }
        }
        return cellData.getStringValue();
    }

    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        RegisterTypeEnum registerTypeEnum;
        try {
            registerTypeEnum = RegisterTypeEnum.valueOf(value);
        } catch (IllegalArgumentException e) {
            return new WriteCellData<>(value);
        }
        return new WriteCellData<>(registerTypeEnum.getName());
    }
}
